package com.mifengs.mall.ui.opencompany;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mifengs.mall.R;
import com.mifengs.commonbase.base.BaseFragment;
import com.mifengs.mall.event.OpenCompanyEvent;
import com.mifengs.mall.event.OpenSelfEvent;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 *
 */
public class AuditFragment extends BaseFragment {


    @BindView(R.id.iv_audit_status)
    ImageView mIvAuditStatus;
    @BindView(R.id.tv_audit_status)
    TextView mTvAuditStatus;
    @BindView(R.id.ll_success)
    LinearLayout mLlSuccess;
    @BindView(R.id.ll_auditing)
    LinearLayout mLlAuditing;
    @BindView(R.id.ll_auditing_fail)
    LinearLayout mLlAuditingFail;
    @BindView(R.id.bt_audit)
    Button mBtAudit;
    @BindView(R.id.tv_audit_idea)
    TextView mTvAuditIdea;

    private AuditStatus mAuditStautus = AuditStatus.AuditIng;
    private String mAuditIdea = "";
    private boolean mIsCompany; // 判断是不是 个体 和 企业
    private boolean mIsPreAudit; //判断是不是 初审



    public enum AuditStatus {
        AuditIng,
        AuditFail,
        AuditSuccess,
        AuditRefuse,
        AuditClose
    }

    public AuditFragment() {
    }

    public static AuditFragment newInstance(AuditStatus auditStatus, String auditIdea ,boolean isCompany ,boolean isPreAudit) {
        AuditFragment fragment = new AuditFragment();
        Bundle args = new Bundle();
        args.putSerializable("auditStautus", auditStatus);
        args.putSerializable("auditIdea", auditIdea);
        args.putBoolean("isCompany",isCompany);
        args.putBoolean("isPreAudit",isPreAudit);
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mAuditStautus = (AuditStatus) getArguments().getSerializable("auditStautus");
            mAuditIdea = getArguments().getString("auditIdea");
            mIsCompany = getArguments().getBoolean("isCompany");
            mIsPreAudit = getArguments().getBoolean("isPreAudit");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_audit, container, false);
        ButterKnife.bind(this, view);
        init();
        return view;
    }

    private void init() {
        switch (mAuditStautus) {
            case AuditSuccess:
                setSuccessView();
                break;
            case AuditIng:
                setAuditIngView();
                break;
            case AuditFail:
                setAuditFailView();
                break;
            case AuditRefuse:
                setAuditRefuse();
                break;
            case AuditClose:
                setAuditClose();
                break;
        }
    }

    /**
     * 审核中的布局
     */
    private void setAuditIngView() {
        mIvAuditStatus.setImageResource(R.drawable.ic_auditing);
        mTvAuditStatus.setText(R.string.audit_ing);
        mLlAuditing.setVisibility(View.VISIBLE);
        mBtAudit.setText(R.string.back_home);
    }

    /**
     * 审核成功的布局
     */
    private void setSuccessView() {
        mIvAuditStatus.setImageResource(R.drawable.ic_audit_success);
        mTvAuditStatus.setText(R.string.audit_success);
        mLlSuccess.setVisibility(View.VISIBLE);
        mBtAudit.setText(R.string.back_home);
    }


    /**
     * 审核失败的布局
     */
    private void setAuditFailView() {
        mIvAuditStatus.setImageResource(R.drawable.ic_audit_fail);
        mTvAuditStatus.setText(R.string.audit_fail);
        mTvAuditStatus.setTextColor(getResources().getColor(R.color.audit_red));
        mLlAuditingFail.setVisibility(View.VISIBLE);
        if (!TextUtils.isEmpty(mAuditIdea))
            mTvAuditIdea.setText(mAuditIdea);
        mBtAudit.setText(R.string.modify_info);
    }

    /**
     * 审核拒绝的布局
     */
    private void setAuditRefuse() {
        mIvAuditStatus.setImageResource(R.drawable.ic_audit_fail);
        mTvAuditStatus.setText(R.string.audit_refuse);
        mTvAuditStatus.setTextSize(15);
        mTvAuditStatus.setTextColor(getResources().getColor(R.color.audit_red));
        mLlAuditingFail.setVisibility(View.GONE);
        mBtAudit.setText(R.string.back_home);
    }

    /**
     * 审核下线或者关闭的布局
     */
    private void setAuditClose() {
        mIvAuditStatus.setImageResource(R.drawable.ic_audit_fail);
        mTvAuditStatus.setText(R.string.audit_close);
        mTvAuditStatus.setTextSize(15);
        mTvAuditStatus.setTextColor(getResources().getColor(R.color.audit_red));
        mLlAuditingFail.setVisibility(View.GONE);
        mBtAudit.setText(R.string.back_home);
    }

    @OnClick(R.id.bt_audit)
    public void onViewClicked() {
        switch (mAuditStautus) {
            case AuditIng:
            case AuditSuccess:
            case AuditRefuse:
            case AuditClose:
                finish();
                break;
            case AuditFail:
                if(!mIsCompany){
                    EventBus.getDefault().post(new OpenSelfEvent(OpenSelfEvent.TOMATERIALSUBPAGE_FAIL));
                }else{
                    if(mIsPreAudit){
                        EventBus.getDefault().post(new OpenCompanyEvent(OpenCompanyEvent.TO_MATERIALSUB_PAGE_FAIL));
                    }else{
                        EventBus.getDefault().post(new OpenCompanyEvent(OpenCompanyEvent.TO_CONTRACT_PAGE_FAIL));
                    }
                }
                break;
        }
    }



    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}
